import java.util.ArrayList;

public class Judicator
{
    public Judicator()
    {
    }

    public ArrayList<Issue> issues = new ArrayList<Issue>();
    public ArrayList<Task> tasks = new ArrayList<Task>();
    public ArrayList<Student> students = new ArrayList<Student>();
    public ArrayList<Team> teams = new ArrayList<Team>();

    public MainWindow mainWindow = null;

    public static void main(String[] args) throws Throwable
    {
        Judicator k = new Judicator();
        new MainWindow(k);
    }


    // STUDENTS

    public void addStudent(Student s)
    {
        students.add(s);
        mainWindow.students.list.add(s.name);
    }

    public void update(Student student)
    {
        int index = students.indexOf(student);
        mainWindow.students.list.setItem(index, student.name);
        update(student.team);
    }

    public Student getStudent(String name)
    {
        for (int i = 0; i < students.size(); i++)
        {
            if (students.get(i).name.compareTo(name) == 0)
            {
                return students.get(i);
            }
        }
        return null;
    }

    public void removeStudents(int[] indices)
    {
        Student[] s = new Student[indices.length];
        for (int i = 0; i < indices.length; i++)
        {
            s[i] = students.get(indices[i]);
        }
        for (int i = 0; i < indices.length; i++)
        {
            removeFromTeam(s[i]);
            students.remove(s[i]);
        }
    }

    public void clearStudents()
    {
        for (int i = 0; i < students.size(); i++)
        {
            removeFromTeam(students.get(i));
        }
        students.clear();
        mainWindow.students.list.removeAll();
    }

    public void removeFromTeam(Student s)
    {
        if (s.team != null)
        {
            s.team.students.remove(s);
            update(s.team);
            s.team = null;
        }
    }


    // TASKS

    public void addTask(Task t)
    {
        tasks.add(t);
        mainWindow.tasks.list.add(t.name + " (" + t.value + ")");
    }

    public void update(Task t)
    {
        int index = tasks.indexOf(t);
        mainWindow.tasks.list.setItem(index, t.name + " (" + t.value + ")");
    }

    public void removeTasks(int[] indices)
    {
        Task[] t = new Task[indices.length];
        for (int i = 0; i < indices.length; i++)
        {
            t[i] = tasks.get(indices[i]);
        }
        for (int i = 0; i < indices.length; i++)
        {
            tasks.remove(t[i]);
        }
    }

    public void clearTasks()
    {
        tasks.clear();
        mainWindow.tasks.list.removeAll();
    }


    // TEAMS

    public void addTeam(Team t)
    {
        teams.add(t);
        mainWindow.teams.list.add(t.getLabel());
    }

    public void update(Team t)
    {
        int index = teams.indexOf(t);
        mainWindow.teams.list.setItem(index, t.getLabel());
    }

    public void removeTeams(int[] indices)
    {
        Team[] t = new Team[indices.length];
        for (int i = 0; i < indices.length; i++)
        {
            t[i] = teams.get(indices[i]);
        }
        for (int i = 0; i < indices.length; i++)
        {
            teams.remove(t[i]);
            t[i].freeStudents();
        }
    }

    public void clearTeams()
    {
        for (int i = 0; i < teams.size(); i++)
        {
            teams.get(i).freeStudents();
        }
        teams.clear();
        mainWindow.teams.list.removeAll();
    }


    // ISSUES

    public void addIssue(Issue i)
    {
        issues.add(i);
        mainWindow.issues.list.add(i.getLabel());
    }

    public void update(Issue i)
    {
        int index = issues.indexOf(i);
        mainWindow.issues.list.setItem(index, i.getLabel());
    }

    public void removeIssues(int[] indices)
    {
        Issue[] s = new Issue[indices.length];
        for (int i = 0; i < indices.length; i++)
        {
            s[i] = issues.get(indices[i]);
        }
        for (int i = 0; i < indices.length; i++)
        {
            issues.remove(s[i]);
            s[i].freeTeams();
        }
    }

    public void clearIssues()
    {
        for (int i = 0; i < issues.size(); i++)
        {
            issues.get(i).freeTeams();
        }
        issues.clear();
        mainWindow.issues.list.removeAll();
    }

    // NUKE'EM

    public void clearAll()
    {
        clearStudents();
        clearTeams();
        clearTasks();
        clearIssues();
        mainWindow.title.setText("");
    }

    public void saveProgress()
    {
        FileFormat f = new FileFormat();
        f.write(this, "temp");
    }

    public String getTitle()
    {
        return mainWindow.title.getText();
    }

    public void setTitle(String s)
    {
        mainWindow.title.setText(s);
    }
}

